<?php
require_once dirname(__FILE__) . "/PUB/Constant.class.php";
require_once "Constant.class.php";

$LoginResult="";

if(isset($_POST["userId"])){
    require_once dirname(__FILE__) . "/PUB/class/tools/ServerFunction.class.php";
    require_once dirname(__FILE__) . "/PUB/class/db/DAB.class.php";
    require_once dirname(__FILE__) . "/PUB/class/check/UserLogin.class.php";

    $userLogin = new UserLogin($_POST["userId"],$_POST["userPasswords"]);

    if(strtoupper(substr($_POST["userId"],0,1))==STAFF_KIND){//教職員登入
        $is_valid=$userLogin->isValidUser("AD");
    }
    else{//學生登入
        $userLogin->db_alias = $db_alias;
        $is_valid=$userLogin->isValidUser("TABLE");
    }

    if($is_valid){//認證成功
        session_start();

        //設定使用者姓名至session
        $_SESSION["empname"]=(strtoupper(substr($_POST["userId"],0,1))!="T" ? $userLogin->userDisplayName : iconv("BIG5","UTF-8",$userLogin->userDisplayName));
        //$_SESSION["empname"]=$userLogin->userDisplayName;

        //設定使用者部門名稱(若是學生則設定班級編號)至session
        $_SESSION["empdeptname"]=(strtoupper(substr($_POST["userId"],0,1))!="T" ? $userLogin->userDeptName : iconv("BIG5","UTF-8",$userLogin->userDeptName));
        //$_SESSION["empdeptname"]=$userLogin->userDeptName;

        //設定使用者所屬之AD群組
        $_SESSION["empgroups"]=(strtoupper(substr($_POST["userId"],0,1))!="T" ? $userLogin->userGroups : iconv("BIG5","UTF-8",$userLogin->userGroups));
        //$_SESSION["empgroups"]=$userLogin->userGroups;
        
        //設定使用者 所在的教室(或處室)編號
        $_SESSION["emproomno"]=(strtoupper(substr($_POST["userId"],0,1))!="T" ? $userLogin->userRoomNo : iconv("BIG5","UTF-8",$userLogin->userRoomNo));

        //若是學生第一次登入需先至更改密碼頁面變更密碼及設定email後才可繼續
        if(strtoupper(substr($_POST["userId"],0,1))!=STAFF_KIND && $_POST["userId"]==$_POST["userPasswords"]){
            $_SESSION["empno"]=$userLogin->username;
            header("location:change_pwd.php");
            exit;
        }
        else{
            $_SESSION["empno"]=$userLogin->username;
        }

        if(isset($_SESSION["PRIVOUS_PAGE"])){
        	// echo "PRIVOUS_PAGE==>" . $_SESSION["PRIVOUS_PAGE"];
            header("location:".$_SESSION["PRIVOUS_PAGE"]);
        }
        else {
            header("location:/OAS/index.php");
        }
    }
    else{
        $LoginResult="登入失敗!!";
    }
}
?>
<html>
    <head>
        <title>Login page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="/PUB/general/workflow.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" language="javascript1.2" src="/PUB/general/jquery-1.4.4.min.js"></script>
        <script type="text/javascript" language="javascript1.2" src="/PUB/general/commscript.js"></script>
        <script type="text/javascript" language="javascript1.2">
            function retrive_win_login()
            {
                var WshNetwork=new ActiveXObject("WScript.Network");
                return WshNetwork.UserName;
            }
            function evt_page_onload(){
                document.myFORM.userId.focus();
                //document.myFORM.userId.value=retrive_win_login();
            }
            function forgot_pwd(){
                if($("#userId").val()==""){
                    alert("請先輸入User ID，此功能僅開放學生使用!!");
                    return false;
                }
                if($("#userId").val().left(1)=="<?php echo STAFF_KIND?>"){
                    alert("此功能僅開放學生使用!!");
                    return false;
                }
            }
			function evt_btnLogin_onclick(){
				if($("#userId").val()=="" || $("#userPasswords").val()==""){
                    alert("UserID 及 Passwords 皆需輸入");
                    return false;
				}
				$("#myFORM").submit();
			}
        </script>
    </head>
    <body background="/PUB/pub_images/login/login_bg.gif" leftmargin="0" topmargin="0"
          marginwidth="0" marginheight="0"
          onload="javascript:evt_page_onload();">
        <form action="login.php" method="post" name="myFORM" id="myFORM">
            <table width="100%" height="500" border="0" cellpadding="0"
                   cellspacing="0">
                <tr>
                    <td valign="middle">
                        <div align="center">
                            <table width="401" height="212" border="0" align="center"
                                   cellpadding="5" cellspacing="2"
                                   background="/PUB/pub_images/login/login_main.gif">
                                <tr>
                                    <td height="46" colspan=2 align=center>&nbsp;</td>
                                </tr>
                                <tr valign="top">
                                    <td width="80">
                                        <div align="right">UserID:</div>
                                    </td>
                                    <td width="231"><input name="userId" type="text" class="input" id="userId"
                                                           size="20" ONFOCUS="this.className='input_onfocus';$('#div_login_result').html('請輸入您的借書證號碼(學生為學號，教職員為電腦登入帳號)');"
                                                           ONBLUR="this.className='input'"></td>
                                </tr>
                                <tr valign="top">
                                    <td>
                                        <div align="right">Password:</div>
                                    </td>
                                    <td><input name="userPasswords" id="userPasswords" type="password" class="input"
                                               size="20" ONFOCUS="this.className='input_onfocus';$('#div_login_result').html('請輸入您[' + document.getElementById('userId').value + ']的密碼(教職員為開機密碼)');"
                                               ONBLUR="this.className='input'"><a style="display: none" href="#" onclick="javascript:forgot_pwd();">(忘記密碼)</a></td>
                                </tr>
                                <tr valign="top">
                                    <td>&nbsp;</td>
                                    <td><input name="btnLogin" type="button" class="button b_normal"
                                               value="Login" onclick="evt_btnLogin_onclick();"> <input name="B2" type="reset"
                                               class="button b_normal" value="Reset"></td>
                                </tr>
                                <tr valign="top">
                                    <td colspan=2 align=center>
                                        <p>
                                        	<div id="div_login_result" style="font-size: 13;color: red;"><b><?php echo $LoginResult; ?></b></div>
                                        </p>
                                    </td>
                            </table>
                        </div>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
